package com.ac.api.component.controller;

import com.ac.api.component.dao.CpVideoMapper;
import com.ac.api.component.vo.CpVideoVo;
import org.springframework.web.bind.annotation.RequestMapping;
import com.base.annotation.SysLog;
import org.springframework.web.bind.annotation.RestController;
import com.base.helper.BaseController;
import com.base.helper.Result;
import com.ac.api.component.entity.CpVideo;
import com.ac.api.component.service.CpVideoService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import javax.validation.Valid;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gjj
 * @since 2024-11-12
 */
@Api(tags = "视频管理")
@RestController
@RequestMapping("/cpVideo")
public class CpVideoController extends BaseController {

    private static final Logger       LOG = LoggerFactory.getLogger(CpVideoController.class);
    @Autowired
    private              CpVideoService entityService;
    @Autowired
    private CpVideoMapper entityMapper;

    @SysLog(action = "findByPage", value = "分页查询视频")
    @ApiOperation(value = "分页查询视频", notes = "分页查询视频")
    @RequestMapping(method = RequestMethod.GET)
    @ApiImplicitParams({@ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面：从1开始", paramType = "query")})
    public Result<IPage<CpVideoVo>> findByPage(final CpVideoVo example, final Page page) {
        IPage<CpVideoVo>        records    = entityService.findByPage(example,page);
        return new Result(true, records);
    }

    @SysLog(action = "delete", value = "删除视频")
    @ApiOperation(value = "删除视频")
    @RequestMapping(value = "{id}", method = {RequestMethod.DELETE})
    @ApiImplicitParam(name = "id", value = "视频ID", required = true, paramType = "path")
    public Result delete(@PathVariable final Integer id) {
        try {
            entityService.deleteVideoById(id);
            return new Result(true, "成功删除视频", null);
        } catch (DataIntegrityViolationException e) {
            LOG.error("删除视频失败", e);
            return new Result(false, "删除视频失败", "该视频不能删除，存在其他关联数据");
        } catch (Exception e) {
            LOG.error("删除视频失败", e);
            return new Result(false, "删除视频失败", e.getMessage());
        }
    }

    @SysLog(action = "one", value = "查询单个视频")
    @ApiOperation(value = "查询单个视频")
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiImplicitParam(name = "id", value = "视频ID", required = true, paramType = "path")
    public Result one(@PathVariable final Integer id) {
        try {
            CpVideoVo entity = entityMapper.findVideoById(id);
            return new Result(true, entity);
        } catch (Exception e) {
            LOG.error("查询单个视频失败", e);
            return new Result(false, new CpVideo());
        }
    }

    @SysLog(action = "add", value = "添加视频")
    @ApiOperation(value = "添加视频")
    @RequestMapping(method = {RequestMethod.POST})
    public Result add(@Valid @RequestBody final CpVideoVo entity, final BindingResult result) {
        try {
            if (result.hasErrors()) {
                Map<String, String> map      = this.getErrors(result);
                String              errorMsg = map.entrySet().iterator().next().getValue();
                return new Result(false, "保存视频失败", errorMsg, map);
            } else {
                CpVideoVo cpVideoVo=entityService.saveVideo(entity);
                if (null == cpVideoVo) {
                    return new Result(false, "","无法找到对应模块和标签");
                }
                return new Result(true, "成功保存视频", null);
            }
        } catch (Exception e) {
            LOG.error("添加视频失败", e);
            return new Result(false, "保存视频失败", e.getMessage());
        }
    }

    @SysLog(action = "update", value = "修改视频")
    @ApiOperation(value = "修改视频")
    @RequestMapping(method = {RequestMethod.PUT})
    public Result update(@Valid @RequestBody final CpVideoVo entity, final BindingResult result) {
        try {
            if (result.hasErrors()) {
                Map<String, String> map      = this.getErrors(result);
                String              errorMsg = map.entrySet().iterator().next().getValue();
                return new Result(false, "修改视频失败", errorMsg, map);
            } else {
                if (null == entity.getId()) {
                    throw new RuntimeException("id不能为空");
                }
                CpVideoVo cpVideoVo=entityService.updateVideo(entity);
                if (null == cpVideoVo) {
                    return new Result(false, "","无法找到对应模块和标签");
                }
                entityService.updateById(entity);
                return new Result(true, "成功修改视频", null);
            }
        } catch (Exception e) {
            LOG.error("修改视频失败", e);
            return new Result(false, "修改视频失败", e.getMessage());
        }
    }

    @ApiOperation(value = "全部视频")
    @RequestMapping(value = "all", method = RequestMethod.GET)
    public Result<List<CpVideo>> all(CpVideo example) {
        List<CpVideo> entitys = entityService.list(example);
        if (null != entitys) {
            return new Result<>(true, entitys);
        }
        return new Result<>(true, Collections.emptyList());
    }

}
